package com.service.impl;

import com.domain.Booktype;
import com.domain.Record;
import com.domain.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mapper.RecordMapper;
import com.service.RecordService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordMapper recordMapper;

    //根据isbn查记录
    @Override
    public Record findByIsbn(String isbn) {
        return recordMapper.findByIsbn(isbn);
    }

    /**
     * 新增借阅记录
     * @param record 新增的借阅记录
     */
    @Override
    public Integer addRecord(Record record) {
        return recordMapper.addRecord(record);
    }

    //显示借阅记录
    @Override
    public PageResult searchRecords(Record record, User user, Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //如果不是管理员，则查询条件中的借阅人设置为当前登录用户
        if(!"ADMIN".equals(user.getRole())){
            record.setBorrower(user.getName());
        }
        Page<Record> page= recordMapper.searchRecords(record);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //借阅排行
    @Override
    public PageResult recordNum(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Booktype> page = recordMapper.recordNum();
        return new PageResult(page.getTotal(),page.getResult());

    }
}
